<?php
/**
 * CreateUser:zlh
 * CreateTime:2022/1/15
 * Remark:该类为员工相关操作控制器
 */
namespace App\Models\Staff;

use AlibabaCloud\SDK\Dingtalk\Vcrm_1_0\Models\DescribeRelationMetaResponseBody\relationMetaDTOList\items\props\dataSource\params\filters;
use App\Consts\BaseConsts\BaseConsts;
use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;
use App\Models\BaseModelV2;
use App\Scopes\MediationStaffScope;
use App\Scopes\MediationStaffUserScope;


class StaffModel extends BaseModelV2
{
    protected $table = TableConsts::CY_STAFF_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_STAFF_TABLE_PRIMARY_KEY;

    protected $fillable = [
        'staff_id',
        'company_id',
        'department_id',
        'staff_name',
        'staff_sex',
        'staff_phone_number',
        'staff_email',
        'staff_main_job_id',
        'staff_other_job_ids',
        'staff_login_account',
        'staff_login_password',
        'staff_last_login_time',
        'staff_ding_talk_id',
        'staff_ding_talk_dept_id',
        'status',
        'created_at',
        'updated_at',
        'staff_type',
        'is_logged',
        'union_id',
        'staff_seat_pwd',
        'staff_seats_number',
        'msi_seats_number'
    ];

    protected static function booted(){
        static::addGlobalScope(new MediationStaffUserScope());
    }

    /**
     * 校验器
     * @param $conditionData
     * @return mixed
     */
    public function isExistByCondition($conditionData): mixed
    {
        return $this->withoutGlobalScopes()->firstOrnew($conditionData)->exists;
    }

    /**
     * 新增员工模板
     * @return int
     */
    public function addStaffModel($staffData) : int
    {
          $this->save($staffData);
          return $this->staff_id;
    }


    /**
     * 获取用户详情
     * @param $staffId
     * @param string[] $select
     * @return mixed
     */
    public function getStaffDetail($staffId, array $select = ["*"]): mixed
    {
        return $this
            ->withoutGlobalScopes()
            ->select($select)
            ->where('staff_id',$staffId)
            ->first();
    }

    public function getStaffInfoByStaffIds($ids,$select = ["*"]){
        return $this
            ->select($select)
            ->whereIn('staff_id',$ids)
            ->get();
    }

    /**
     * 获取用户列表
     * @param $query
     * @param $pageSize
     * @return mixed
     */
    public function getStaffList($query,$pageSize): mixed
    {
        return $query
            ->withoutGlobalScopes()
            ->latest()
            ->Paginate($pageSize);
    }

    /**
     * 编辑员工基本信息
     * @param $staffId
     * @param $staffData
     * @return mixed
     */
    public function editStaffBasic($staffId,$staffData): mixed
    {
        return $this->where('staff_id',$staffId)->update($staffData);
    }

    /**
     * 批量删除
     * @param $staffIds
     * @param $staffData
     * @return mixed
     */
    public function editStaffIds($staffIds,$staffData): mixed
    {
        return $this->withoutGlobalScopes()->whereIn('staff_id',$staffIds)->update($staffData);
    }

    /**
     * 获取用户信息
     * @param $account
     * @param $companyId
     * @return mixed
     */
    public function getStaffPwdByStaffAccount($account,$companyId): mixed
    {
        return $this
            ->withoutGlobalScopes()
            ->select(['staff_id','company_id','staff_login_password'])
            ->where('staff_login_account',$account)
            ->where('company_id',$companyId)
            ->where('staff_status',BaseConsts::STATUS_START)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->first();
    }

    public function getStaffsInfo(array $staffIds,array $select = ["*"]){
        return $this->whereIn("staff_id",$staffIds)
            ->select($select)
            ->get();
    }

    public function getDingTalkIdIsCompanyBind(mixed $dingTalkUserId, $companyId)
    {
        return $this
            ->select(['staff_id'])
            ->where('company_id',$companyId)
            ->where('staff_status',BaseConsts::STATUS_START)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->where('staff_dingding_account',$dingTalkUserId)
            ->first();
    }

    public function getStaffDetailByDingTalkId(mixed $dingTalkUserId)
    {
        return $this
            ->select(['staff_id','staff_name','staff_dingding_account','company_id'])
            ->where('staff_dingding_account',$dingTalkUserId)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->first();
    }

    public function getDingTalkUserIdByUserIds($userIds,$select = ["*"])
    {
        return $this->select($select)
            ->withoutGlobalScopes()
            ->whereIn('staff_id',$userIds)
            ->get();
    }

    public function getAllStaffBySelect($select = ["*"])
    {
        return $this->select($select)
            ->get();
    }


}
